package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.SpringTemplateEngine;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
public class IndexController {

    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private SpringTemplateEngine templateEngine;

    @GetMapping("/createHtml")
    @ResponseBody
    public Result createHtml(){
        //数据
        List<Map> list1Map = getData();
        //封装数据的对象
        Context context = new Context();
        context.setVariable("list",list1Map);

        //输出流 编码
        Writer out = null;
        try {
            //out = new OutputStreamWriter(
            //      new FileOutputStream("D:\\temp\\index.html"), "utf-8")
            out = new PrintWriter("D:\\temp\\index.html","utf-8");//写
            //对首页进行静态化处理  Thymeleaf： 模板技术
            //参数1：模板
            //参数2：数据
            //参数3：输出 （哪里
            //进行静态化处理
            templateEngine.process("index/index",context,out);//UTF-8 nacos配置了thymeleaf
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null!=out)
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return Result.ok();
    }

    //进入首页
    @GetMapping("/")
    public String index(Model model) {
        List<Map> list1Map = getData();
        model.addAttribute("list",list1Map);
        return "index/index";
    }

    //获取三级分类的数据
    private List<Map> getData(){
        List<BaseCategoryView> baseCategoryList = productFeignClient.getBaseCategoryList();

        //准备一个list
        List<Map> list1Map = new ArrayList<>();

        //Stream流 分组  Long:category1Id  1 :  List<BaseCategoryView>:60
        //Stream流 分组  Long:category1Id  2 :  List<BaseCategoryView>:85 - 60
        //Stream流 分组  Long:category1Id  3 :  List<BaseCategoryView>:```
        //按照getCategory1Id 进行分组
        Map<Long, List<BaseCategoryView>> category1IdMap = baseCategoryList.stream()
                .collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
        int index = 0;
        for (Map.Entry<Long, List<BaseCategoryView>> category1IdEntry : category1IdMap.entrySet()) {
            //一及分类
            List<BaseCategoryView> category2IdList = category1IdEntry.getValue();
            Map category1Result = new HashMap<>();
            category1Result.put("index",++index);
            category1Result.put("categoryId",category1IdEntry.getKey());
            category1Result.put("categoryName",category2IdList.get(0).getCategory1Name());
            /////////////////////////////////////////////////////////////////////////////////////////
            //二级分类集合
            List<Map> list2Map = new ArrayList<>();
            Map<Long, List<BaseCategoryView>> category2IdMap =
                    category2IdList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            for (Map.Entry<Long, List<BaseCategoryView>> category2IdEntry : category2IdMap.entrySet()) {
                List<BaseCategoryView> category3IdList = category2IdEntry.getValue();
                Map category2Result = new HashMap<>();
                category2Result.put("categoryId",category2IdEntry.getKey());
                category2Result.put("categoryName",category3IdList.get(0).getCategory2Name());
                //////////////////////////////////////////////////////////////////////////////////////
                //三级分类集合
                List<Map> list3Map = category3IdList.stream().map(baseCategoryView3 -> {
                    Map category3Result = new HashMap<>();
                    category3Result.put("categoryId", baseCategoryView3.getCategory3Id());
                    category3Result.put("categoryName", baseCategoryView3.getCategory3Name());
                    return category3Result;
                }).collect(Collectors.toList());
                category2Result.put("categoryChild",list3Map);
                list2Map.add(category2Result);
            }
            category1Result.put("categoryChild",list2Map);
            list1Map.add(category1Result);
        }
        return list1Map;

    }


}

